package ToYesterday.Day17_insertBST_S;


import org.junit.Test;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}


public class Solution {
    TreeNode node;
    public void init(){
        node = new TreeNode(4);
        node.left=new TreeNode(2);
        node.right=new TreeNode(7);
        node.left.left = new TreeNode(1);
        node.left.right = new TreeNode(3);
    }
    public TreeNode insertIntoBST(TreeNode root, int val) {
        init();
        TreeNode p =node;
        while (true){
            if (p==null){
                p=new TreeNode(val);
                root = p;
                break;
            }

            if (p.val<val&&p.right!=null){
                p=p.right;
            }else if (p.val>val&&p.left!=null){
                p=p.left;
            }

            if (p.val<val&&p.right==null){
                p.right = new TreeNode(val);
                break;
            }else if (p.val>val&&p.left==null){
                p.left = new TreeNode(val);
                break;
            }

        }
        return node;
    }


    @Test
    public void TestNode(){
        TreeNode newnode = insertIntoBST(new TreeNode(2),5);
        System.out.println(newnode.right.left.val);
    }
}
